//
//  RechargeController.swift
//  SCA
//
//  Created by tjun on 2025/9/15.
//

import UIKit

class RechargeController: BaseController {
    
    let model: RechargeModel
    
    init(model: RechargeModel) {
        self.model = model
        super.init(nibName: nil, bundle: nil)
        self.modalPresentationStyle = .fullScreen
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationBar.title = "充值USDT".localizedString
        
        let scrollView = UIScrollView(frame: CGRect(x: 0, y: navBarHeight, width: view.width, height: screenHeight - navBarHeight)).addTo(view)
        scrollView.keyboardDismissMode = .onDrag
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.alwaysBounceVertical = true
        scrollView.contentInsetAdjustmentBehavior = .never
        
        let contentSize = CGSize(width: scrollView.width, height: scrollView.height)
        
        scrollView.contentSize = contentSize
        
        let contentView = UIView().addTo(scrollView)
        contentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.size.equalTo(contentSize)
        }
        
        let qrImage = UIImage.generateQRCode(from: model.address, size: 240.scaled)
        let qrCodeImageView = UIImageView(image: qrImage)
        contentView.addSubview(qrCodeImageView)
        qrCodeImageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(20)
            make.width.height.equalTo(240.scaled)
        }
        
        let networkAddressView = RechargeNetworkAddressView().addTo(contentView)
        networkAddressView.networkNameLabel.text = model.network
        networkAddressView.addressValueLabel.text = model.address
        networkAddressView.layer.cornerRadius = 10
        networkAddressView.layer.masksToBounds = true
        networkAddressView.layer.borderWidth = 0.7
        networkAddressView.copyButton.addTarget(self, action: #selector(copyButtonTapped), for: .touchUpInside)
        networkAddressView.layer.borderColor = UIColorMake(0xD2D2D2).cgColor
        networkAddressView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.top.equalTo(qrCodeImageView.snp.bottom).offset(30)
        }
        
        let warningView = UIView().addTo(contentView)
        warningView.addCorner(radius: 10)
        warningView.backgroundColor = UIColorMake(0x8E40F2).withAlphaComponent(0.2)
        warningView.snp.makeConstraints { make in
            make.left.right.equalTo(networkAddressView)
            make.top.equalTo(networkAddressView.snp.bottom).offset(5)
        }
        
        let warningLabel = UILabel(font: UIFontMake(14), textColor: .white).addTo(warningView)
        warningLabel.numberOfLines = 0
        let attributedText = NSMutableAttributedString()
        if let image = UIImage(named: "warning") {
            let attachment = NSTextAttachment()
            attachment.image = image
            let imageHeight: CGFloat = 16
            attachment.bounds = CGRect(x: 0, y: (warningLabel.font.capHeight - imageHeight) / 2, width: imageHeight, height: imageHeight)
            attributedText.append(NSAttributedString(attachment: attachment))
            attributedText.append(NSAttributedString(string: " ")) // 图标和文字之间加空格
        }
        
        let text = model.hint
        let textAttr = NSAttributedString(string: text, attributes: [
            .font: UIFont.systemFont(ofSize: 14),
            .foregroundColor: UIColor.white
        ])
        attributedText.append(textAttr)
        
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 3
        paragraphStyle.alignment = .left
        attributedText.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: attributedText.length))
        warningLabel.attributedText = attributedText
        warningLabel.snp.makeConstraints { make in
            make.left.right.right.equalToSuperview().inset(AppStyle.margin.left)
            make.top.equalTo(10)
            make.bottom.equalTo(-10)
        }
        
    }
    
    @objc func copyButtonTapped() {
        UIPasteboard.general.string = model.address
        HUD.showMessage("复制成功".localizedString)
    }
    
    
}
